package com.base.po.coupon_sdk;

import com.alibaba.fastjson.JSON;
import com.base.helper.WebHelper;
import com.base.po.coupon.*;
import com.base.utils.Produce;
import org.apache.commons.collections.map.HashedMap;

import java.util.Map;

/**
 * 365优惠券
 * */
public class CouponInfoUtil {

    /**
     * 查询商品信息
     *
     * @param productId 产品编号
     * */
    public static QueryProductResponse queryProduct(String productId){
        QueryProductResponse response = null;

        try {
            Map<String, String> map = new HashedMap();
            map.put("app_id", CouponConfig.APPID);
            map.put("product_id", productId);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            String sign = CouponUtil.getSign(map, CouponConfig.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtil.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfig.QUERY_PRODUCT, param, null, null);
            response = JSON.toJavaObject(JSON.parseObject(result), QueryProductResponse.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 查询商品库存
     *
     * @param productId 产品编号
     * */
    public static QueryStockResponse queryStock(String productId){
        QueryStockResponse response = null;

        try {
            Map<String, String> map = new HashedMap();
            map.put("app_id", CouponConfig.APPID);
            map.put("product_id", productId);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            String sign = CouponUtil.getSign(map, CouponConfig.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtil.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfig.QUERY_STOCK, param, null, null);
            response = JSON.toJavaObject(JSON.parseObject(result), QueryStockResponse.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 创建订单
     *
     * @param productId 产品编号
     * @param orderId 订单ID
     * @param count 数量
     * */
    public static PayOrderResponse payOrder(String productId, String orderId, String count){
        PayOrderResponse response = null;

        try {
            Map<String, String> map = new HashedMap();
            map.put("app_id", CouponConfig.APPID);
            map.put("product_id", productId);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            map.put("out_trade_no", orderId);
            map.put("count", count);

            String sign = CouponUtil.getSign(map, CouponConfig.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtil.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfig.PAY_ORDER, param, null, null);
            response = JSON.toJavaObject(JSON.parseObject(result), PayOrderResponse.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 订单状态查询
     *
     * @param orderNo API订单号
     * */
    public static QueryOrderResponse queryOrder(String orderNo){
        QueryOrderResponse response = null;

        try {
            Map<String, String> map = new HashedMap();
            map.put("app_id", CouponConfig.APPID);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            map.put("order_no", orderNo);

            String sign = CouponUtil.getSign(map, CouponConfig.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtil.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfig.QUERY_ORDER, param, null, null);
            response = JSON.toJavaObject(JSON.parseObject(result), QueryOrderResponse.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 申请退款
     *
     * @param orderNo API订单号
     * */
    public static RefundOrderResponse refundOrder(String orderNo){
        RefundOrderResponse response = null;

        try {
            Map<String, String> map = new HashedMap();
            map.put("app_id", CouponConfig.APPID);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            map.put("order_no", orderNo);

            String sign = CouponUtil.getSign(map, CouponConfig.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtil.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfig.REFUND_ORDER, param, null, null);
            response = JSON.toJavaObject(JSON.parseObject(result), RefundOrderResponse.class);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }
}
